package com.augmented.jellybean;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

public class AugmentedJellybeanHomeActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.augmented_jellybean_home);

		Button btnSearch	 = (Button) findViewById(R.id.btnSearch);
		Button btnNearby	 = (Button) findViewById(R.id.btnNearby);
		Button btnDeals		 = (Button) findViewById(R.id.btnDeals);
		Button btnRefined	 = (Button) findViewById(R.id.btnRefinedSearch);
		final EditText searchInput = (EditText) findViewById(R.id.search_input);

		searchInput.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				searchInput.setHint("");
			}
		});

		// Listening to the Search event
		btnSearch.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				//Starting a new Intent
				Intent nextScreen = new Intent(getApplicationContext(), AugmentedJellybeanMapScreenActivity.class);

				String query = searchInput.getText().toString();
				if (query != null) {
					query.replace(' ', '+');
					query.trim();
				}

				//Sending data to another Activity
				nextScreen.putExtra("search_input", searchInput.getText().toString());
				Log.i("Search query for: ", query);

				InputMethodManager imm = (InputMethodManager)getSystemService(
						Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(searchInput.getWindowToken(), 0);

				startActivity(nextScreen);
			}
		});

		/**
		 * The Nearby event shifts application to a new activity, hopefully a list implemented with specific categories
		 */
		btnNearby.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				//Starting a new Intent
				Intent nextScreen = new Intent(getApplicationContext(), CategoryListActivity.class);

				startActivity(nextScreen);
			}
		});

		// Listening to the Deals event
		btnDeals.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				//Starting a new Intent
				Intent nextScreen = new Intent(getApplicationContext(), AugmentedJellybeanDealsScreenActivity.class);

				startActivity(nextScreen);
			}
		});

		// Listening to the Markers event
		btnRefined.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				//Starting a new Intent
				Intent nextScreen = new Intent(getApplicationContext(), AugmentedJellybeanMapScreenActivity.class);

				startActivity(nextScreen);
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if ((keyCode == KeyEvent.KEYCODE_BACK))
		{
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
}
